package cn.stylefeng.guns.modular.rest.basic;

import cn.stylefeng.guns.modular.storage.model.result.StorageOutResult;
import cn.stylefeng.guns.modular.storage.service.StorageOutService;
import groovyjarjarpicocli.CommandLine;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;

import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *客户控制器
 */
@Controller
@RequestMapping("/rest/customer")
public class restCustomerController extends BaseController {

    @Autowired
    private UserAop userAop;

    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private StorageOutService storageOutService;

    private Lock lock = new ReentrantLock();
    /**
     * 客户列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public ResponseData list() {
    	User user = userAop.getUser();
        return ResponseData.success(basicCustomerService.findAvailableList(user.getDeptId(),user.getDeliverId(),user.getOperateId()));
    }


    /**
     * 客户是否一人一单
     */
    @ResponseBody
    @RequestMapping("/findListByWarehouseId")
    public ResponseData findListByWarehouseId(@Param("warehouseId") String warehouseId ,@Param("storageOutNumber") String storageOutNumber) {
        lock.lock();
        String listByWarehouseId = basicCustomerService.findListByWarehouseId(warehouseId);
        User user = userAop.getUser();
        try{
            if(StringUtils.isNotBlank(listByWarehouseId)  && "是".equals(listByWarehouseId) ){
                StorageOutResult byUserAndStorageOutNumber = storageOutService.findByUserAndStorageOutNumber(user.getUserId(), storageOutNumber);
                if(byUserAndStorageOutNumber.getAssignee() == null ){
                    //更新拣选人，拣选时间
                    storageOutService.updateUserByStorageOutNumber(user.getUserId(),user.getName(),new Date() ,storageOutNumber);

                    return ResponseData.success();

                }else if(byUserAndStorageOutNumber.getAssignee() != null && byUserAndStorageOutNumber.getAssignee().longValue() != user.getUserId().longValue()) {
                    return ResponseData.error(byUserAndStorageOutNumber.getAssigneename()+"正在拣货！！");
                }
            }else if (StringUtils.isNotBlank(listByWarehouseId)  && "否".equals(listByWarehouseId)){
                return ResponseData.success("");
            }
        }catch (Exception e){
            return ResponseData.error("错误");
//            e.printStackTrace();
        }finally {
            lock.unlock();
        }
        return ResponseData.success(true);
    }
    
}
